﻿using System;
using System.Linq;
using BingoClientModel;
using BingoHallServerConsole.Business.Account;
using BingoHallServerConsole.Model;
using BingoServerCommon.Common;
using BingoServerCommon.DbModel;
using Dapper;
using MySql.Data.MySqlClient;

namespace BingoHallServerConsole.Business.Jigsaw
{
	/// <inheritdoc />
	public class CS_GetJigsawInfo : Package
	{


		public CS_GetJigsawInfo(EnumPackCommand mainid) : base(mainid)
		{
		}

		public CS_GetJigsawInfo(byte[] buffer, int len) : base(buffer, len)
		{
		}

		public override Package Clone(byte[] bytes)
		{
			return new CS_GetJigsawInfo(bytes, bytes.Length);
		}





		/// <summary>
		/// 查询拼图具体情况
		/// </summary>
		public override void Excute(Session session)
		{
			var type = this.ReadInt();

			MySqlConnection conn = Program.ConnectionPool.getConnection(); //从连接池拿到一个连接
			string sql = $"SELECT * from jigsaw j WHERE j.type = {type};";

			var package = new Package(EnumPackCommand.SC_GetJigsawInfo_Ret);

			var jigsaw = conn.Query<jigsaw>(sql).ToList();

			if (jigsaw.Count > 0)
			{
				//0表示成功，其余是失败
				package.Write((byte)EnumReqRetStatus.Success);

				package.Write(type);
				package.Write(jigsaw.Count);
				for (int i = 0; i < jigsaw.Count; i++)
				{
					package.Write(jigsaw[i].height_num ?? 0);
					package.Write(jigsaw[i].width_num ?? 0);
				}

			}
			else
			{
				//非0是失败
				package.Write((byte)EnumReqRetStatus.Fail0);
			}

			session.Send(package);

			Program.ConnectionPool.returnConnection(conn);//连接用完回收
		}


	}
}
